package com.iqoo.store.servlet;

import com.iqoo.store.model.User;
import com.iqoo.store.dao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取表单数据
        UserDao userDao = new UserDao();
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // 验证用户名和密码
        User user = userDao.login(username);
        if(user != null && user.getPassword().equals(password)) {
            // 登录成功
            request.getSession().setAttribute("user", user);
            response.sendRedirect("index.jsp");
        } else {
            // 登录失败
            request.setAttribute("error", "用户名或密码错误");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 处理 GET 请求（如果需要）
    }
}
